#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD ""
#define DATABASE "tcmu"

void printResult(MYSQL *mysqlPrint);

int main()
{
    MYSQL connection;
    mysql_init(&connection);
    mysql_real_connect(&connection, HOST, USERNAME, PASSWORD, DATABASE, 0, NULL, 0);
    mysql_query(&connection, "set names 'utf8'");
    int execute_result = mysql_query(&connection, "select * from kv where `Key` = '123456'");
    if (execute_result)
    {
        printf("Error happens.\n");

    }
    else
    {
        printResult(&connection);
    }
    mysql_close(&connection);
    return 0;
}

void printResult(MYSQL *mysqlPrint)
{
    MYSQL_RES * result;
    int numFields = 0;
    int numRows = 0;
    MYSQL_FIELD * field;
    MYSQL_ROW row;
    int i = 0;
    result = mysql_store_result(mysqlPrint);
    numFields = mysql_num_fields(result);
    numRows = mysql_num_rows(result);
    while(field = mysql_fetch_field(result))
        printf("%s\t", field->name);
    printf("\n");
    if(result)
    {
        while(row = mysql_fetch_row(result))
        {
            for(i = 0; i < numFields; i++)
            {
                printf("%s\t", row[i]);
            }
            printf("\n");
        }
    }
    mysql_free_result(result);
}
